<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    public $uid = 0;
    public function index() {
        $this->display();
    } 
    public function chat() {
        $this->uid = I('uid');
        if($this->uid == 0) $this->error("请选择用户");
        session('uid', $this->uid);
        $this->assign('uid',$this->uid);

        $list[0]['text'] = "ThinkPHP框架";
        $list[0]['right'] = 0;
        $list[0]['imgsrc'] = C('HOST').'/Public/images/pic.jpg';

        $list[1]['text'] = "Ci框架";
        $list[1]['right'] = 0;
        $list[1]['imgsrc'] = C('HOST').'/Public/images/pic.jpg';

        $list[2]['text'] = "YII框架";
        $list[2]['right'] = 1;
        $list[2]['imgsrc'] = C('HOST').'/Public/images/pic.jpg';

        $list[3]['text'] = "laravel框架";
        $list[3]['right'] = 0;
        $list[3]['imgsrc'] = C('HOST').'/Public/images/pic.jpg';
        $arr['json'] = json_encode($list);
        if(count($list) < 1) $arr['json'] = '[]';
        $this->assign('arr',$arr);
        $this->display();
    } 

    function bind() { 
        $uid = session('uid');
        $client_id = I('client_id');
        $gateway = new \Org\Util\Gateway();
        $gateway->bindUid($client_id, $uid);
        $count = $gateway->getAllClientCount();
        $message = '绑定成功' . $uid . '-' . $client_id.'-共有'.$count.'个人在线';
        echo $message;
        // $gateway->sendToUid($uid, $message);
    } 

    function message() { 
        $to_uid = I('uid');
        $message = I('msg');
        $gateway = new \Org\Util\Gateway();
        $data['text'] = $message;
        $data['from_uid'] = session('uid');
        $data['to_uid'] = $to_uid;
        $data['right'] = 0;
$data['imgsrc'] = C('HOST').'/Public/images/pic.jpg';
        if ($gateway->isUidOnline($to_uid)) {
            // 判断接受小时着是否在线，在线执行发送消息
            $gateway->sendToUid($to_uid, json_encode($data));
        }else{
            $data['text'] .= '[对方不在线]';
        }

$data['right'] = 1;
        $gateway->sendToUid($data['from_uid'], json_encode($data)); // 发给自己 
        
        echo json_encode($data);
    }

    function more_msg(){
        $list[0]['text'] = "1";
        $list[0]['right'] = 0;
        $list[0]['imgsrc'] = C('HOST').'/Public/images/pic.jpg';

        $list[1]['text'] = "2";
        $list[1]['right'] = 1;
        $list[1]['imgsrc'] = C('HOST').'/Public/images/pic.jpg';

        $list[2]['text'] = "3";
        $list[2]['right'] = 1;
        $list[2]['imgsrc'] = C('HOST').'/Public/images/pic.jpg';

        $list[3]['text'] = "4";
        $list[3]['right'] = 0;
        $list[3]['imgsrc'] = C('HOST').'/Public/images/pic.jpg';
        echo json_encode($list);
    }
}